﻿using Entity;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using CommandService;
using Tools;
using Entity.FrameEntity;
using SysFramework;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using System.Net.NetworkInformation;
using System.Collections.Immutable;
using Entity.ViewEntity;

namespace BLL
{
    public class ProductSetBLL : PubMethod
    {
        Commands cmd;
        ReturnResult rsl;
        OperateModel oper;
        List<OperateModel> operList;
        public ProductSetBLL()
        {
            cmd = new Commands();
        }
        /// <summary>
        /// 有效生产工序
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ReturnResult GetGongXuList(string conditions)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select id,cCode,cName,orderNum,cMemo from fun_CommonData_Info c ");
                sb.Append(" where isdel=0 and  cType='Gx'");
                sb.Append(" and cCode is not null and cCode !='' ");
                sb = CreateSelectCondition(sb,conditions);
                sb.Append(" order by convert(decimal(10,2),orderNum)  ");
                var gxTable = cmd.GetDataTable(sb.ToString());
                rsl.Data = cmd.TableToDictionary(gxTable);
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 工序数据添加方法
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public ReturnResult UpdateGongXu(string datas, string userCode)
        {
            rsl = new ReturnResult();
            try
            {
                oper = new OperateModel();
                var obj = JsonConvert.DeserializeObject<DbCommonData>(datas);
                obj.status = "Y";
                obj.cType = "GX";
                obj.isSys = "1";
                obj.isDel = 0;
                string sql = "select * from fun_CommonData_Info where cCode='" + obj.cCode + "' and cType='GX'";
                var qTable=cmd.GetDataTable(sql);
                if (obj.id == null || obj.id == 0)
                {
                    if (qTable.Rows.Count > 0)
                    {
                        rsl.Message = "编码存在重复"; return rsl;

                    }
                    obj.createtime=getNow();
                    obj.createuser=userCode;
                    rsl = cmd.Add(obj);
                }
                else
                {
                    bool cf = false;
                    foreach (DataRow row in qTable.Rows)
                    {
                        if (row["id"].ToString() != obj.id.ToString())
                        {
                            cf = true; break;
                        }

                    }
                    if (cf)
                    {
                        rsl.Message = "编码存在重复"; return rsl;
                    }
                    oper.Types = OperateModel.OperateType.Edit;
                    oper.Conditions = "id";
                    oper.Data = obj;
                    rsl = cmd.Edit(oper);
                }

            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
               
        /// <summary>
        /// 根据工序编码获取工序表单数据
        /// </summary>
        /// <param name="gxCode"></param>
        /// <returns></returns>
        public ReturnResult GetGongFormByCode(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                var obj = JsonConvert.DeserializeObject<JObject>(condition);
                StringBuilder sb = new StringBuilder();
                sb.Append(" gxCode='" + obj["s-gxCode"].ToString() + "' and isdel=0 ");
                if (!string.IsNullOrWhiteSpace(obj["s-proCode"].ToString()))
                {
                    var pArr = obj["s-proCode"].ToString().Split(',');
                    string pCodes = "";
                    foreach (string p in pArr)
                    {
                        if (!string.IsNullOrWhiteSpace(p))
                        {
                            pCodes += string.Format("'{0}',", p);
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(pCodes.TrimEnd(',')))
                    {
                        sb.Append(" and relationCode in(" + pCodes.TrimEnd(',') + ")");
                    }
                }
                var list = cmd.PageList("gy_FormStandard_fields", pageIndex, pageSize, sb.ToString(), "id desc", ref pageCount);
                rsl.Result = true;
                rsl.Data = list;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 更新工序表单
        /// </summary>
        /// <param name="datas">数据表单内容</param>
        /// <returns></returns>
        public ReturnResult UpdateGxForm(string datas, string userCode)
        {
            rsl = new ReturnResult();
            operList = new List<OperateModel>();
            try
            {
                StringBuilder sb = new StringBuilder();
                var json = JsonConvert.DeserializeObject<JObject>(datas);
                if (string.IsNullOrWhiteSpace(json["relationCode"].ToString()))
                {
                    rsl.Message = "没有设置关联存货";
                    return rsl;
                }
                sb.Append("select id from gy_FormStandard_fields where gxCode='" + json["gxCode"].ToString() + "'");
                sb.Append("  and fieldCode='" + json["fieldCode"].ToString() + "'");
                var qTable = cmd.GetDataTable(sb.ToString());
                if (!string.IsNullOrWhiteSpace(json["id"].ToString()) && json["id"].ToString() != "0")
                {
                    #region 修改操作 修改只允许单条修改
                    var db = JsonConvert.DeserializeObject<DbFormStandardFields>(datas);
                    oper = new OperateModel();
                    oper.Data = db;
                    oper.Conditions = "id";
                    oper.Types = OperateModel.OperateType.Edit;
                    operList.Add(oper);
                    #endregion
                }
                else
                {
                    #region 添加操作
                    if (qTable.Rows.Count > 0)
                    {
                        rsl.Message = "字段编码已存在";
                        return rsl;
                    }

                    var c_arr = json["relationCode"].ToString().Split(',');
                    var n_arr = json["relationName"].ToString().Split(',');
                    for (int i = 0; i < c_arr.Length; i++)
                    {
                        if (!string.IsNullOrWhiteSpace(c_arr[i]))
                        {
                            var db = JsonConvert.DeserializeObject<DbFormStandardFields>(datas);
                            db.relationCode = c_arr[i];
                            db.relationName = n_arr[i];
                            db.createtime = getNow();
                            db.createuser = userCode;
                            db.isdel = 0;
                            oper = new OperateModel();
                            oper.Data = db;
                            oper.Types = OperateModel.OperateType.Add;
                            operList.Add(oper);
                        }
                    }
                    #endregion
                }

                if (operList.Count > 0)
                {
                    rsl = cmd.MoreUpdate(operList);
                }
                else
                {
                    rsl.Message = "没有数据";
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 获取预设工艺存货
        /// </summary>
        /// <param name="gxCode"></param>
        /// <returns></returns>
        public ReturnResult GetProcessProduct()
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select distinct relationCode,relationName from gy_FormStandard_fields ");
                sb.Append(" where isdel=0");
                var table = cmd.GetDataTable(sb.ToString());
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 加载工序表单
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public ReturnResult loadGxForm(string datas)
        {
            rsl = new ReturnResult();
            try
            {
                List<Dictionary<string, object>> dlist = new List<Dictionary<string, object>>();
                var obj = JsonConvert.DeserializeObject<JObject>(datas);
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from gy_FormStandard_fields where isdel=0");
                sb.Append(" and gxCode='" + obj["gxCode"].ToString() + "' and relationCode='" + obj["proCode"].ToString() + "'");
                sb.Append(" order by groupCode,fieldOrder asc");
                var table = cmd.GetDataTable(sb.ToString());
                string groupCode = "";
                List<string> reList = new List<string>();
                foreach (DataRow r in table.Rows)
                {

                    if (!reList.Contains(r["groupCode"].ToString()))
                    {
                        var dic = new Dictionary<string, object>();
                        dic["gCode"] = r["groupCode"].ToString();
                        dic["singleRow"] = r["singleRow"].ToString();
                        var dr = table.Select("groupCode='" + r["groupCode"].ToString() + "'");
                        List<Dictionary<string, object>> clist = new List<Dictionary<string, object>>();
                        foreach (DataRow sr in dr)
                        {
                            var c_dic = new Dictionary<string, object>();
                            c_dic["fieldCode"] = sr["fieldCode"].ToString();
                            c_dic["fieldName"] = sr["fieldName"].ToString();
                            c_dic["fieldType"] = sr["fieldType"].ToString();
                            if (sr["fieldType"].ToString() == "fxlk" && !string.IsNullOrWhiteSpace(sr["dSoure"].ToString()))
                            {
                                var dtable = cmd.GetDataTable(sr["dSoure"].ToString());
                                string values = "";
                                foreach (DataRow qr in dtable.Rows)
                                {
                                    values += string.Format("{0},{1};", qr[0].ToString(), qr[1].ToString());
                                }
                                c_dic["dSoure"] = values.TrimEnd(';');
                            }
                            else
                            {
                                c_dic["dSoure"] = "";
                            }
                            clist.Add(c_dic);
                        }
                        dic["gfields"] = clist;
                        dlist.Add(dic);
                        reList.Add(r["groupCode"].ToString());
                    }
                }
                rsl.Data = dlist;
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        public ReturnResult loadProcessGx(string relationCode)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select cCode,cName from fun_CommonData_Info where cCode in( ");
                sb.Append(" select distinct gxCode from gy_FormStandard_fields where ");
                sb.Append("relationCode='" + relationCode + "' and isdel=0) and isdel=0");
                sb.Append(" and status='Y' order by orderNum asc");
                var table = cmd.GetDataTable(sb.ToString());
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        
        public ReturnResult loadPlanProcessGx(string plCode)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select cCode,cName from fun_CommonData_Info where cCode in( ");
                sb.Append(" select distinct gxCode from pi_ProductionPlan_process where ");
                sb.Append("plCode='" + plCode + "' and isdel=0) and isdel=0");
                sb.Append(" and status='Y' order by orderNum asc");
                var table = cmd.GetDataTable(sb.ToString());
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message=ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 保存工艺标准数据
        /// </summary>
        /// <param name="mDatas">主数据</param>
        /// <param name="fDatas">表单数据</param>
        /// <param name="userCode">登录人员</param>
        /// <returns></returns>
        public ReturnResult SaveGyStandard(string mDatas, string fDatas, string userCode)
        {
            rsl = new ReturnResult();
            operList = new List<OperateModel>();
            try
            {
                var mJson = JsonConvert.DeserializeObject<JObject>(mDatas);
                var fJson = JsonConvert.DeserializeObject<JObject>(fDatas);
                StringBuilder sb = new StringBuilder();
                DbProcessStandard gy;
                string keyCode = "";
                bool isAdd = true;
                if (string.IsNullOrWhiteSpace(mJson["keyCode"].ToString()))
                {
                    keyCode = getTimeString();
                }
                else
                {
                    keyCode = mJson["keyCode"].ToString();
                }
                if (string.IsNullOrWhiteSpace(mJson["versionCode"].ToString()))
                {
                    rsl.Message = "工艺版本号不允许为空"; return rsl;
                }
                sb.Append("select * from gy_ProcessStandard_info where gxCode='" + mJson["gxCode"].ToString() + "'");
                sb.Append(" and relationCode='" + mJson["relationCode"].ToString() + "' ");
                sb.Append(" and keyCode='" + mJson["keyCode"].ToString() + "' and versionCode='" + mJson["versionCode"].ToString() + "'");
                var qtable = cmd.GetDataTable(sb.ToString());
                if (qtable.Rows.Count > 0)
                {
                    isAdd = false;
                }

                #region 工艺数据
                foreach (JProperty obj in fJson.Properties())
                {
                    gy = new DbProcessStandard();
                    gy.createtime = getNow();
                    gy.creatuser = userCode;
                    gy.keyCode = keyCode;
                    gy.isdel = 0;
                    gy.memo = mJson["memo"].ToString();
                    gy.gyName = mJson["gyName"].ToString();
                    gy.versionCode = mJson["versionCode"].ToString();
                    gy.gxCode = mJson["gxCode"].ToString();
                    gy.fieldCode = obj.Name;
                    gy.value = obj.Value.ToString();
                    oper = new OperateModel();
                    oper.Data = gy;
                    if (isAdd)
                    {
                        gy.relationName = mJson["relationName"].ToString();
                        gy.relationCode = mJson["relationCode"].ToString();
                        oper.Types = OperateModel.OperateType.Add;
                    }
                    else
                    {
                        oper.Types = OperateModel.OperateType.Edit;
                        oper.Conditions = "keyCode,fieldCode,gxCode";
                    }
                    operList.Add(oper);
                }
                #endregion

                rsl = cmd.MoreUpdate(operList);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 根据工序编码获取工序表单数据
        /// </summary>
        /// <param name="gxCode"></param>
        /// <returns></returns>
        public ReturnResult ProductGyStandardList(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                var obj = JsonConvert.DeserializeObject<JObject>(condition);
                StringBuilder sb = new StringBuilder();
                sb.Append(" 1=1 ");
                sb = CreateSelectCondition(sb, condition);
                var list = cmd.PageList("V_gy_ProductStandard", pageIndex, pageSize, sb.ToString(), "keyCode desc", ref pageCount);
                rsl.Result = true;
                rsl.Data = list;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        public ReturnResult UpdateStatus(string cCode, string status)
        {
            rsl = new ReturnResult();
            operList = new List<OperateModel>();
            try
            {
                StringBuilder sb = new StringBuilder();
                DbCommonData db = new DbCommonData();
                db.cCode = cCode;
                db.leader = status;
                oper = new OperateModel();
                oper.Data = db;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "cCode";
                rsl = cmd.Edit(oper);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 根据工序编码获取工序表单数据
        /// </summary>
        /// <param name="gxCode"></param>
        /// <returns></returns>
        public ReturnResult ProGyStandardList(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                if (string.IsNullOrWhiteSpace(condition))
                {
                    rsl.Message = "获取不到选中存货/品类";
                    return rsl;
                }
                StringBuilder sb = new StringBuilder();
                var json = JsonConvert.DeserializeObject<JObject>(condition);
                //按存货编码查询工艺
                #region 检索条件
                sb.Append(" 1=1 ");
                foreach (JProperty obj in json.Properties())
                {
                    if (!string.IsNullOrWhiteSpace(obj.Value.ToString()))
                    {
                        if (!string.IsNullOrWhiteSpace(sb.ToString()))
                        {
                            sb.Append(" and ");
                        }
                        sb.Append(obj.Name + " like '%" + obj.Value.ToString() + "%' ");
                    }
                }
                #endregion
                var proSql=sb.ToString();
                var list = cmd.PageList("V_gy_ProductStandard", pageIndex, pageSize, sb.ToString(), "keyCode desc", ref pageCount);//按存货编码查询工艺
                if (list.Count<=0)
                {
                    //按存货大类查询工艺
                    #region  获取存货大类
                    sb.Clear();
                    sb.Append("select typeCode from fun_Inventory_Info where proCode='" + json["relationCode"].ToString() + "'");
                    var table = cmd.GetDataTable(sb.ToString());
                    if (table.Rows.Count > 0)
                    {
                        proSql=proSql.Replace(json["relationCode"].ToString(), table.Rows[0]["typeCode"].ToString());
                    }
                    else
                    {
                        rsl.Message = "无法获取当前存货所属大类";
                        return rsl;
                    }
                    #endregion
                    list = cmd.PageList("V_gy_ProductStandard", pageIndex, pageSize, proSql, "keyCode desc", ref pageCount);//按存货大类查询工艺
                }
                rsl.Result = true;
                rsl.Data = list;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }


        /// <summary>
        /// 根据编码获取工艺绩效标准
        /// </summary>
        /// <param name="keyCode">工艺编码</param>
        /// <param name="proCode">存货编码</param>
        /// <returns></returns>
        public ReturnResult GetStandardByCode(string datas)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(datas);
                if (dic.ContainsKey("plCode") && !string.IsNullOrWhiteSpace(dic["plCode"]))
                {
                    #region 存在计划编码 数值需要在工艺快照获取数据
                    sb.Append("select * from pi_ProductionPlan_process where isdel=0 ");
                    sb.Append(" and plCode='" + dic["plCode"] + "' and gxCode='" + dic["gxCode"] + "' ");
                    #endregion
                }
                else
                {
                    sb.Append("select s.*,f.fieldType from gy_ProcessStandard_info s ");
                    sb.Append("inner join gy_FormStandard_fields f on f.fieldCode=s.fieldCode ");
                    sb.Append("where s.keyCode='" + dic["keyCode"] + "' ");
                    if (dic.ContainsKey("relationCode") && !string.IsNullOrWhiteSpace(dic["relationCode"]))
                    {//存在版本号
                        sb.Append(" and s.relationCode='" + dic["relationCode"] + "'");
                    }
                    sb.Append(" and s.gxCode='" + dic["gxCode"] + "' and s.isdel=0");
                    if (dic.ContainsKey("version") && !string.IsNullOrWhiteSpace(dic["version"]))
                    {//存在版本号
                        sb.Append(" and s.versionCode='" + dic["version"] + "'");
                    }
                }
                rsl.Data = cmd.TableToDictionary(cmd.GetDataTable(sb.ToString()));
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 删除工艺标准
        /// </summary>
        /// <param name="keyCode"></param>
        /// <param name="gxCode"></param>
        /// <param name="relationCode"></param>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public ReturnResult DelGyStandard(string keyCode, string gxCode, string relationCode, string userCode)
        {
            rsl = new ReturnResult();
            try
            {
                DbProcessStandard s = new DbProcessStandard();
                s.gxCode = gxCode;
                s.keyCode = keyCode;
                s.relationCode = relationCode;
                s.isdel = 1;
                s.delMsg = string.Format("{0}({1})", userCode, getNow());
                oper = new OperateModel();
                oper.Conditions = "keyCode,gxCode,relationCode";
                oper.Data = s;
                oper.Types = OperateModel.OperateType.Edit;
                rsl = cmd.Edit(oper);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 保存工艺主数据
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public ReturnResult SaveGyMainData(string datas)
        {
            rsl = new ReturnResult();
            try
            {
                var db = JsonConvert.DeserializeObject<DbProcessStandard>(datas);
                oper = new OperateModel();
                oper.Data = db;
                oper.Types = OperateModel.OperateType.Edit;
                oper.Conditions = "keyCode";
                rsl = cmd.Edit(oper);
            }
            catch (Exception ex)
            {

            }
            return rsl;
        }
   

        public ReturnResult DelFGXData(string dataId)
        {
            rsl = new ReturnResult();
            StringBuilder sb = new StringBuilder();
            try
            {
                DbCommonData db = new DbCommonData();
                db.id = int.Parse(dataId);
                db.isDel = 1;
                oper = new OperateModel();
                oper.Data = db;
                oper.Conditions = "id";
                oper.Types = OperateModel.OperateType.Edit;
                rsl = cmd.Edit(oper);
            }
            catch (Exception ex)
            {
                rsl.Message=ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 生产班组表单数据
        /// </summary>
        /// <param name="gxCode"></param>
        /// <returns></returns>
        public ReturnResult GetGxGroupList(string pageIndex, string pageSize, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();
            try
            {
                var obj = JsonConvert.DeserializeObject<JObject>(condition);
                StringBuilder sb = new StringBuilder();
                sb.Append(" 1=1 ");
                sb = CreateSelectCondition(sb, condition);
                var list = cmd.PageList("V_GongXuGroup_info", pageIndex, pageSize, sb.ToString(), "parentCode desc", ref pageCount);
                rsl.Result = true;
                rsl.Data = list;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 工序用户列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="gxCode"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public ReturnResult GongXuUserList(string pageIndex, string pageSize, string gxCode,ref string pageCount)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(" isdel=0 and status='Y'");
                var list = cmd.PageList("V_UserInfo", pageIndex, pageSize, sb.ToString(), "id desc", ref pageCount);
                sb.Clear();
                sb.Append("select * from pi_GongXuUser_set where gongxu='"+ gxCode + "'");
                var ugTable = cmd.GetDataTable(sb.ToString());
                for (int i=0;i<list.Count;i++)
                {
                    var q = ugTable.Select("usCode='"+ list[i]["userCode"] +"'");
                    if (q.Length > 0)
                    {
                        list[i]["gx"] = "1";
                    }
                    else
                    {
                        list[i]["gx"] = "0";
                    }
                }
                rsl.Data = list;
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 工序人员添加
        /// </summary>
        /// <param name="userCodes">添加人员编号</param>
        /// <param name="gxCode">关联工序</param>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public ReturnResult AddGongXuUser(string userCodes,string gxCode,string userCode)
        { 
            rsl=new ReturnResult();
            operList = new List<OperateModel>();
            try
            {
                if (string.IsNullOrWhiteSpace(userCodes) || string.IsNullOrWhiteSpace(gxCode))
                {
                    rsl.Message = "人员或工序信息不能为空";return rsl;
                }
                var uArr=userCodes.Split(',');
                DbGongXuUser gxu;
                foreach (var u in uArr)
                {
                    if (!string.IsNullOrEmpty(u))
                    {
                        gxu=new DbGongXuUser();
                        gxu.createtime = getNow();
                        gxu.createuser = userCode;
                        gxu.GongXu = gxCode;
                        gxu.usCode = u;
                        oper=new OperateModel();
                        oper.Data = gxu;
                        oper.Types=OperateModel.OperateType.Add;
                        operList.Add(oper);
                    }
                }
                rsl=cmd.MoreUpdate(operList);
            }
            catch (Exception ex)
            { 
               rsl.Message=ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 删除工序用户
        /// </summary>
        /// <param name="gxCode"></param>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public ReturnResult DelGongXuUser(string gxCode, string userCode)
        {
            rsl = new ReturnResult();
            try
            {
                if (string.IsNullOrWhiteSpace(userCode) || string.IsNullOrWhiteSpace(gxCode))
                {
                    rsl.Message = "人员或工序信息不能为空"; return rsl;
                }
                StringBuilder sb = new StringBuilder();
                sb.Append("delete from pi_GongXuUser_set where usCode='"+ userCode +"'");
                sb.Append(" and GongXu='" + gxCode + "'");
                rsl=cmd.UpdateSql(sb.ToString());
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
    }
}
